//
//  CancelAccountView.swift
//  注销账号
//
//  Created by 千策万划-mini on 2025/4/29.
//

import Foundation

class CancelAccountView: UIView {

    var blockConfirmCancelAccount:(() -> Void)?
    
    // MARK: - lazy
    lazy var viewBg: UIImageView = {
        let result = UIImageView()
        result.widthHeight = XY(355, 468)
        result.isUserInteractionEnabled = true
        result.image = UIImage(named: "icon_cancel_account_bg")
        return result
    }()
    lazy var btnClose: UIButton = {
        let result = UIButton()
        result.widthHeight = XY(40+16*2, 40+30*2)
        result.tag = 1
        result.addTarget(self, action: #selector(btnClick(_:)), for: .touchUpInside)
        return result
    }()
    lazy var labelTitle: OutlineLabel = {
        let result = OutlineLabel()
        result.resetView(
            title: "Logout Reminder",
            textColor: COLOR_WHITE(),
            font: UIFont.customBoldFont(size: 24),
            shadowColor: UIColor(hex: "#4105A0"),
            outlineColor: UIColor(hex: "#3B0B9A")
        )
        return result
    }()
    lazy var viewContent: UIImageView = {
        let result = UIImageView()
        result.image = UIImage(named: "icon_login_input_bg")
        result.widthHeight = XY(275, 228)
        return result
    }()
    lazy var labelContent: UILabel = {
        let result = UILabel()
        result.textAlignment = .center
        result.font = UIFont.customRegularFont(size: 14)
        result.textColor = COLOR_LABEL()
        result.numberOfLines = 0
        return result
    }()
    lazy var imageAgreeBg: UIImageView = {
        let result = UIImageView()
        result.widthHeight = XY(274, 40)
        result.image = UIImage(named: "icon_login_agree_bg")
        result.isUserInteractionEnabled = true
        return result
    }()
    lazy var btnAgree: UIButton = {
        let result = UIButton()
        result.widthHeight = XY(18+11*2, 40)
        result.setImage(UIImage(named: "icon_agree_uncheck"), for: .normal)
        result.setImage(UIImage(named: "icon_agree_check"), for: .selected)
        result.tag = 4
        result.addTarget(self, action: #selector(btnClick(_:)), for: .touchUpInside)
        return result
    }()
    lazy var labelAgree: UILabel = {
        let result = UILabel()
        result.textColor = UIColor(hex: "#D8CDFF")
        result.font = UIFont.customMediumFont(size: 13)
        return result
    }()
    
    lazy var btnConfirm: UIButton = {
        let result = UIButton()
        result.widthHeight = XY(132, 46)
        result.setImage(UIImage(named: "icon_pop_confirm"), for: .normal)
        result.tag = 2
        result.addTarget(self, action: #selector(btnClick(_:)), for: .touchUpInside)
        return result
    }()
    lazy var labelConfirm: OutlineLabel = {
        let result = OutlineLabel()
        result.resetView(
            title: "Confirming",
            textColor: COLOR_WHITE(),
            font: UIFont.customBoldFont(size: 16),
            shadowColor: UIColor(hex: "#4105A0"),
            outlineColor: UIColor(hex: "#3B0B9A")
        )
        return result
    }()
    lazy var btnCancel: UIButton = {
        let result = UIButton()
        result.widthHeight = XY(132, 46)
        result.setImage(UIImage(named: "icon_pop_cancel"), for: .normal)
        result.tag = 3
        result.addTarget(self, action: #selector(btnClick(_:)), for: .touchUpInside)
        return result
    }()
    lazy var labelCancel: OutlineLabel = {
        let result = OutlineLabel()
        result.resetView(
            title: "Cancel",
            textColor: COLOR_WHITE(),
            font: UIFont.customBoldFont(size: 16),
            shadowColor: UIColor(hex: "#4105A0"),
            outlineColor: UIColor(hex: "#3B0B9A")
        )
        return result
    }()

    // MARK: - init
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.widthHeight = XY(SCREEN_WIDTH, SCREEN_HEIGHT)
        self.backgroundColor = UIColor(hex: "000000", alpha: 0.8)
        self.addSubView()
    }

    @available(*, unavailable)
    required init?(coder _: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    // MARK: - 添加
    func addSubView() {
        addSubview(viewBg)
        viewBg.addSubview(btnClose)
        viewBg.addSubview(labelTitle)
        viewBg.addSubview(viewContent)
        viewContent.addSubview(labelContent)
        
        viewBg.addSubview(imageAgreeBg)
        imageAgreeBg.addSubview(labelAgree)
        imageAgreeBg.addSubview(btnAgree)
        
        viewBg.addSubview(btnConfirm)
        btnConfirm.addSubview(labelConfirm)
        viewBg.addSubview(btnCancel)
        btnCancel.addSubview(labelCancel)

        resetView()
    }

    // MARK: - 点击事件
    @objc func btnClick(_ sender: UIControl) {
        feedback()
        sender.addClickAnimate()
        switch sender.tag {
        case 1:
            hide()
            break
        case 2: // confirm
            if let block = blockConfirmCancelAccount {
                block()
            }
            break
        case 3: // Cancel
            hide()
            break
        case 4: // agreement
            sender.isSelected = !sender.isSelected
            break
        default:
            break
        }
    }
    
    // 显示
    func show() {
        if let window = WindowManager.shared.getCurrentWindow() {
            window.endEditing(true)
            // 设置初始状态（比如从底部开始）
            alpha = 0
            viewBg.transform = CGAffineTransform(translationX: 0, y: 50)
            window.addSubview(self)
            // 添加弹出动画
            UIView.animate(withDuration: 0.3, delay: 0, options: .curveEaseOut) {
                self.alpha = 1
                self.viewBg.transform = .identity
            }
        }
    }

    // 隐藏
    func hide() {
        // 添加消失动画
        UIView.animate(withDuration: 0.3, delay: 0, options: .curveEaseIn) {
            self.alpha = 0
            self.viewBg.transform = CGAffineTransform(translationX: 0, y: 50)
        } completion: { _ in
            self.removeFromSuperview()
            // 重置 transform，以便下次显示
            self.viewBg.transform = .identity
        }
    }

    // MARK: - 重置界面
    func resetView() {
        viewBg.centerXCenterY = XY(SCREEN_WIDTH/2, SCREEN_HEIGHT/2)
        btnClose.rightTop = XY(viewBg.width, 0)
        labelTitle.centerXTop = XY(viewBg.width/2, 25)
        viewContent.centerXTop = XY(viewBg.width/2, 110)
        
        let str = "After canceling your account, your loan\nrecords, repayment information, and\naccount data will be permanently\ndeleted and cannot be restored. If there\nare outstanding payments, pending\ndisputes, or orders under warranty,\ncancellation will lead to overdue risks\nand affect personal credit. Please\nconfirm that all business processing has\nbeen completed. Do you want to\ncontinue to cancel?"
        let paragraphStyle = NSMutableParagraphStyle()
        let font = labelContent.font!
        let minLineHeight: CGFloat = font.lineHeight * 0.8
        paragraphStyle.minimumLineHeight = minLineHeight
        paragraphStyle.maximumLineHeight = minLineHeight
        paragraphStyle.alignment = labelContent.textAlignment
        let attributedString = NSAttributedString(
            string: str,
            attributes: [
                .paragraphStyle: paragraphStyle,
                .font: font
            ]
        )
        labelContent.attributedText = attributedString
        labelContent.sizeToFit()
        labelContent.centerXCenterY = XY(viewContent.width/2, viewContent.height/2+2)
        
        imageAgreeBg.centerXTop = XY(viewBg.width/2, viewContent.bottom+6)
        btnAgree.leftCenterY = XY(0, imageAgreeBg.height/2)
        labelAgree.fitTitle("I have read and agree to the above", variable: 0)
        labelAgree.leftCenterY = XY(btnAgree.right-4, btnAgree.centerY)
        
        btnConfirm.rightTop = XY(viewBg.width/2-6, imageAgreeBg.bottom+13)
        labelConfirm.centerXCenterY = XY(btnConfirm.width/2, btnConfirm.height/2)
        
        btnCancel.leftCenterY = XY(viewBg.width/2+6, btnConfirm.centerY)
        labelCancel.centerXCenterY = XY(btnCancel.width/2, btnCancel.height/2)
    }
}
